package com.zhuss.product.processor;

import java.util.*;

import com.zhuss.bean.vo.car.car.DetailCarVo;
import com.zhuss.bean.vo.car.driver.DetailDriverVo;
import com.zhuss.bean.vo.product.train.DetailTrainVo;
import com.zhuss.car.processor.cache.CarCache;
import com.zhuss.car.processor.cache.DriverCache;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.product.processor.cache.StrategyTrainCache;
import com.zhuss.product.processor.cache.TrainCache;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.product.train.UpdateTrainBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.product.db.dao.ext.TrainDao;
import com.zhuss.product.db.entity.Train;
import com.zhuss.db.entity.OpLog;

import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;

/**
 * @description: 车次修改
 * @author: zhushanshuo
 * @create: Mon Jan 04 08:55:12 CST 2021
 **/
@Component("trainUpdate")
public class TrainUpdate extends AbstractProcessor<Object, UpdateTrainBo> implements Processor<Object, UpdateTrainBo> {

    @Autowired
    private TrainDao trainDao;

    @Autowired
	@Qualifier("carCache")
    private CarCache carCache;

	@Autowired
	@Qualifier("trainRead")
	private TrainRead trainRead;

    @Autowired
	@Qualifier("trainCache")
	private TrainCache trainCache;

	@Autowired
	@Qualifier("driverCache")
    private DriverCache driverCache;

	@Autowired
	@Qualifier("strategyTrainCache")
	private StrategyTrainCache strategyTrainCache;

    /**
     * 校验
     */
    @Override
    public void check(UpdateTrainBo bo) throws DzcxException {
    	Long trainId = bo.getTrainId();
    	if (trainId == null || trainId <= 0l) {
    		throw new DzcxException(ExceptionCode.车次id为空);
    	}

    	if (bo.getCarId() == null || bo.getCarId() <=0l) {
    		throw new DzcxException(ExceptionCode.车辆ID为空);
		}

    	if (bo.getDriverId() == null || bo.getDriverId() <=0l) {
    		throw new DzcxException(ExceptionCode.驾驶员ID为空);
		}

		DetailCarVo detailCarVo = carCache.get(bo.getCarId());
		DetailDriverVo detailDriverVo = driverCache.get(bo.getDriverId());

		if (!detailCarVo.getCompanyId().equals(detailDriverVo.getCompanyId())) {
			throw new DzcxException(ExceptionCode.所选车辆和司机不属于一家公司);
		}

		bo.setDetailCarVo(detailCarVo);

    }

    /**
     * 操作方法
     */
    @Override
    public Object process(UpdateTrainBo param) throws DzcxException {
    	Train record = new Train();
		BeanUtils.copyProperties(param,record);
		record.setPeopleQuantity(Math.toIntExact(param.getDetailCarVo().getPeopleQuantity()));
    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	trainDao.updateByPrimaryKeySelective(record);

		DetailTrainVo vo = trainRead.detail(param.getTrainId());
		param.setName(vo.getName());
		trainCache.clear(param.getTrainId());

		strategyTrainCache.clear(vo.getLineStrategyId());
        return null;
    }

    /**
     * 操作日志
     */
	@Override
	public Object log(UpdateTrainBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.商品.key());
    	record.setAction(LogActionEnum.车次修改.key());
    	record.setTargetId(param.getTrainId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
